from __future__ import absolute_import
from pyprotobuf.outputbuffer import OutputBuffer
import logging


class CodeGenerator(object):
    logger = logging.getLogger("pyprotobuf.CodeGenerator")
    
    def __init__(self, frame=None):
        super(CodeGenerator, self).__init__()
        self.output = OutputBuffer()
        self.arguments = None
        self._generate_headers = True

    @classmethod
    def get_title(cls):
        return ''

    @classmethod
    def add_arguments(self, parser):
        """
            :type parser: argparse.ArgumentParser
        """
        pass

    def set_arguments(self, arguments):
        self.arguments = arguments


    def get_argument(self, *args):
        return getattr(self.arguments, *args)


    def set_generate_headers(self, b):
        # disable file headers (for testing output)
        # TODO: we should be testing the ast or individual methods instead of whole output diff
        self._generate_headers = b


    def get_generate_headers(self):
        return self._generate_headers


    def generate_file(self, filenode, header=True):
        """ Generate  a FileNode.

            TODO: deprecate header kwargs. we should be testing the ast
        """
        raise NotImplementedError

    def call_visitor_method(self, node):
        cls = node.__class__.__name__

        # if defined, call the named visit_* function in subclasses
        if hasattr(self, 'visit_%s' % cls):
            getattr(self, 'visit_%s' % cls)(node)